package com.jic.member.impl.power;

import java.lang.Long;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import com.jic.common.base.vo.AdminOperate;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.api.manage.SysDeptApi;
import com.jic.manager.api.rest.ManagerChildApi;
import com.jic.manager.request.ChildNameRequest;
import com.jic.manager.request.SysDeptRequest;
import com.jic.manager.response.SysDeptResponse;
import com.jic.member.entity.power.MememberLegalRule;
import com.jic.member.entity.power.MevipRule;
import com.jic.member.mapper.power.MememberLegalRuleMapper;
import com.jic.member.mapper.power.MevipRuleMapper;
import com.jic.member.service.power.MevipRuleService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 会员vip权益关联表
 * @author : 
 * @email : 
 * @since : 2020-04-06 02:05:20
 * @version : v1.0.0
 */
@Service
public class MevipRuleServiceImpl implements MevipRuleService {

    @Resource
    private MevipRuleMapper mevipRuleMapper;
    @Resource
    MememberLegalRuleMapper memberLegalRuleMap;
    //@Resource
    @Autowired
    private ManagerChildApi managerChildApi;
    @Autowired
    private SysDeptApi sysDeptApi;

    @Override
    public long insert(MevipRule mevipRule) {
        long flag=0;
        AdminOperate adminOperate = mevipRule.getAdminOperate();
        mevipRule.setCreatorName(adminOperate.getOperateName());
        mevipRule.setDeleteFlag(0);
        mevipRule.setVipId(mevipRule.getId().longValue());
        mevipRule.setCreator(adminOperate.getOperator());
        mevipRule.setCreateTime(new Date());
        mevipRule.setDeptType(adminOperate.getDeptType().getCode().longValue());
        //先删除，后保存
        mevipRuleMapper.deleteByDeptId(mevipRule.getDeptId(),mevipRule.getId().longValue());
        mevipRule.getRuleIds().forEach(items->{
            mevipRule.setLegalRuleId(items);
            mevipRuleMapper.insert(mevipRule);
        });
        return flag;
    }

    @Override
    public long delete(Long id) {
        return mevipRuleMapper.delete(id);
    }

    @Override
    public long update(MevipRule mevipRule) {
        return mevipRuleMapper.update(mevipRule);
    }

    @Override
    public MevipRule queryByPrimaryKey(Long id){
        return mevipRuleMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<MevipRule> query(MevipRule mevipRule){
        List<MevipRule>mevipRuleList = mevipRuleMapper.query(mevipRule);
        mevipRuleList.forEach(items->{
            MememberLegalRule mevip=memberLegalRuleMap.queryByPrimaryKey(items.getLegalRuleId());
            items.setLegalRuleName(mevip.getLegalRuleName());
        });
        return  mevipRuleList;
    }

    @Override
    public PageResult<MevipRule> queryPage(MevipRule mevipRule, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        mevipRule.setDeleteFlag(0);
        AdminOperate admin = mevipRule.getAdminOperate();
        mevipRule.setDeptId(admin.getDeptId());
        //如果是商家或者店铺登录查询适用范围
        SysDeptRequest query= new SysDeptRequest();
        //查询适用范围
//        MemMerchantInfoResponse memMerchantInfoResponse = sysDeptApi.getGrowhtScopeInfo(admin.getMerchantIds().get(0));
//        if(admin.getDeptType().getCode()==2 || admin.getDeptType().getCode()==3){
//
//            if(null !=memMerchantInfoResponse && memMerchantInfoResponse.getWhetherApplication()==1){
//                //如果适用范围设置的是商家 -- 店铺登录
//                if (memMerchantInfoResponse.getScopeApplication()== 1 && admin.getDeptType().getCode()==3){
//                    //查询店铺的所属上级商家deptid
//                    query.setDeptType(3);
//                    query.setBusinessId(admin.getBusinessId());
//                    List<SysDeptResponse> resultSysDept = sysDeptApi.query(query);
//                    if(CollectionUtils.isNotEmpty(resultSysDept)){
//                        mevipRule.setDeptId(resultSysDept.get(0).getPid());
//                    }
//                    //如果适用范围设置的是店铺 -- 商家登录
//                }else if(memMerchantInfoResponse.getScopeApplication()== 2 && admin.getDeptType().getCode()==2){
//                    //查询当前商家下所有的店铺deptid
//                    List<Long> storeIds = admin.getStoreIds();
//                    List<Long> deptIds = new ArrayList<Long>();
//                    storeIds.forEach(itms->{
//                        query.setDeptType(3);
//                        query.setBusinessId(itms);
//                        List<SysDeptResponse> resultSysDept = sysDeptApi.query(query);
//                        if (CollectionUtils.isNotEmpty(resultSysDept)){
//                            deptIds.add(resultSysDept.get(0).getId());
//                        }
//                    });
//                    mevipRule.setDeptId(null);
//                    mevipRule.setDeptIds(deptIds);
//                }
//            }
//        }
        //查看子平台设置不需要setDeptId
        if (null != mevipRule.getIsMom() && mevipRule.getIsMom() == 1){
            mevipRule.setDeptId(null);
        }
        if (null !=mevipRule.getMerchantOrStoreName() && "" !=mevipRule.getMerchantOrStoreName()){
            SysDeptRequest sysDept=new SysDeptRequest();
            sysDept.setDeptName(mevipRule.getMerchantOrStoreName());
            List<Long>sysdept = new ArrayList<Long> ();
            List<SysDeptResponse> resultSys = sysDeptApi.query(sysDept);
            if(CollectionUtils.isNotEmpty(resultSys)){
                resultSys.forEach(itmes -> {
                    sysdept.add(itmes.getId());
                });
            }
            if (resultSys.size()==0){
                sysdept.add(0L);
                mevipRule.setDepts(sysdept);
            }else {
                mevipRule.setDepts(sysdept);
            }
        }
        List<MevipRule> list = mevipRuleMapper.queryPage(startIndex, pageQuery.getLimit(), mevipRule);
        long count = mevipRuleMapper.queryPageCount(mevipRule);
            list.forEach(items->{
                RestResult<String> name = sysDeptApi.queryDeptNameById(items.getDeptId());
                items.setDeptName(name.getData());
//                items.setScopeApplication(memMerchantInfoResponse.getScopeApplication());
            });
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }
    /*
    查询商家/店铺vip设置所有列表
    * */
    @Override
    public PageResult<MevipRule> queryMerchantOrStorePage(MevipRule mevipRule, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        mevipRule.setDeleteFlag(0);
        AdminOperate admin = mevipRule.getAdminOperate();
        if (null !=mevipRule.getMerchantOrStoreName() && mevipRule.getMerchantOrStoreName() != ""){
            List<Long> sysDeptList = sysDeptApi.querymMerchantOrStoreListByName(mevipRule.getMerchantOrStoreName());
            if (CollectionUtils.isNotEmpty(sysDeptList)){
                mevipRule.setMerchantOrStoreIds(sysDeptList);
            }else {
                sysDeptList.add(0L);
                mevipRule.setMerchantOrStoreIds(sysDeptList);
            }
        }
        //如过是子平台登录查看 1查询子平台所属下级 2
        if (admin.getDeptType().getCode()==1){
            List<Long> childList = sysDeptApi.queryChildListById(admin.getDeptId());
            if (null !=childList && childList.size()>0){
            mevipRule.setChildIds(childList);
         }
    }
    List<MevipRule> list = mevipRuleMapper.queryMerchantOrStorePage(startIndex, pageQuery.getLimit(), mevipRule);
    long count = mevipRuleMapper.queryMerchantOrStorePageCount(mevipRule);
        list.forEach(items->{
            RestResult<String> name = sysDeptApi.queryDeptNameById(items.getDeptId());
            ChildNameRequest childNameRequest = new ChildNameRequest();
            childNameRequest.setDeptId(items.getDeptId());
            RestResult<String> childName = sysDeptApi.queryChildNameByDeptId(childNameRequest);
            items.setDeptName(childName.getData());
            items.setMerchantOrStoreName(name.getData());
        });
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
        }

      public long deleteByDeptId(Long deptId,Long vipId) {
        return mevipRuleMapper.deleteByDeptId(deptId,vipId);
        }


}